<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Http\Requests\MessageRequest;
use App\Jobs\SendEmailMessage;
use App\Model\Admin\MessageModel;
use App\Model\ConfigModel;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    //
    /**
     * 执行留言
     *
     * @param MessageRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveMessage(MessageRequest $request)
    {
        $Message = new MessageModel();
        $Message->name = $request->get('name');
        $Message->email = $request->get('email');
        $Message->product = $request->get('product');
        $Message->phone = $request->get('phone');
        $Message->message = $request->get('message');
        $Message->ip = $request->ip();

        if ($Message->save()){

//            $email = (new SendEmailMessage($Message->toArray()))->onQueue('email');
            dispatch(new SendEmailMessage($Message->toArray()));
            return response()->json([
                'code' => 0,
                'msg' => 'Success',
                'url' => 'leave-message-success'
            ]);
        } else{
            return response()->json(['code' => 1, 'msg' => 'Sorry, there is something wrong with the system']);
        }
    }

    // 留言成功页面
    public function leaveMessageSuccess()
    {
        $data['seo'] = ConfigModel::where('config_type','contact')
            ->first();
        return view('front.message-success')->with('data',$data);
    }
}
